package win.demonlegion.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QrcodeUtil {
    // convert BufferedImage to byte[]
    public static byte[] toByteArray(BufferedImage bi, String format) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(bi, format, stream);
            byte[] bytes = stream.toByteArray();
            return bytes;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    // convert byte[] to BufferedImage
    public static BufferedImage toBufferedImage(byte[] bytes) {
        try {
            InputStream is = new ByteArrayInputStream(bytes);
            BufferedImage bi = ImageIO.read(is);
            return bi;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static String encode2Base64(String content, int width, int height) {
        Map<EncodeHintType, Object> encodeHints = new HashMap<>();
        encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
            BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix);
            return Base64Util.toBase64(toByteArray(qrcode, "png"));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static BufferedImage encode2BufferedImage(String content, int width, int height) {
        Map<EncodeHintType, Object> encodeHints = new HashMap<>();
        encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
            return MatrixToImageWriter.toBufferedImage(bitMatrix);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static InputStream encode2InputStream(String content, int width, int height) {
        Map<EncodeHintType, Object> encodeHints = new HashMap<>();
        encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, encodeHints);
            BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(qrcode, "png", os);
            return new ByteArrayInputStream(os.toByteArray());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
}
